//
//  DataDownloadViewController.m
//  MaskMachine
//
//  Created by yuanyuan on 15-3-2.
//  Copyright (c) 2015年 hanjiaxue. All rights reserved.
//

#import "DataDownloadViewController.h"
#import "DZNSegmentedControl.h"
#import "AccountTableViewCell.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "DetailsWebViewController.h"
#import "DataImageViewController.h"
#import "DataTuiDetailsViewController.h"
#import "YoungAllData.h"
#import "GMDCircleLoader.h"
@interface DataDownloadViewController ()<YoungDelegate>{
    int a;
}
@property(nonatomic,retain)NSMutableArray *zArrayData;
@property(nonatomic,retain)NSMutableArray *zArrayImage;
@end

static NSString *accountCell = @"Cell";

@implementation DataDownloadViewController
@synthesize zArrayData,zArrayImage;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden=YES;
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    navTitle.text = @"推广素材";
    
    
    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate = self;
    [data gainGoodsList:@"" andPageID:@"1" andPerPage:@"15" andSearch:@"" andAgentID:[[NSUserDefaults standardUserDefaults] objectForKey:@"userID"]];
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];
    
    a = 0;
    
    _control = [[DZNSegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"推广素材",@"图片素材",nil]];
    _control.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"segmentBG"]];
    _control.frame = CGRectMake(0, 64, self.view.frame.size.width, 39);
    _control.delegate = self;
    _control.selectedSegmentIndex = 0;
    _control.bouncySelectionIndicator = YES;
    _control.tintColor = [UIColor redColor];
    
    _control.showsCount = NO;
    [_control addTarget:self action:@selector(selectedSegment:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_control];
    
    
    
}
#pragma mark - 获取商品类型回调
-(void)goodsList:(NSDictionary *)sign{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
    zArrayData = [[NSMutableArray alloc]initWithArray:[sign objectForKey:@"data"]];
    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate = self;
    [data gainGetType];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }
}
#pragma mark - 商品列表回调失败
-(void)requistFail{

    [GMDCircleLoader hideFromView:self.view animated:YES];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

#pragma mark - 获取图片素材分类
-(void)getType:(NSDictionary *)sign{
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
    zArrayImage = [[NSMutableArray alloc]initWithArray:[sign objectForKey:@"data"]];
    [self createUItableView];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

-(void)createUItableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.control.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-64-self.control.frame.size.height-50)];
    _tableView.backgroundColor = [UIColor clearColor];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[AccountTableViewCell class] forCellReuseIdentifier:accountCell];
    
}
#pragma mark - 返回
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)downloadDataTuiMessages:(NSArray *)array{
    
    
    self.arrays = [NSArray arrayWithArray:array];
    [self.tableView reloadData];
    
    
}

-(void)downloadDataImgMessage:(NSArray *)array{
    NSLog(@"%@ 图片列表",array);
    
    self.imageArray = [NSArray arrayWithArray:array];
    [self.tableView reloadData];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.control.selectedSegmentIndex == 0) {
        a = 0;
        return [zArrayData count];
    }else if(self.control.selectedSegmentIndex == 1){
        a = 1;
        return [zArrayImage count];
    }
    
    return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AccountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:accountCell];
    
    if (a == 0) {
        cell.titleLabel.text = [[zArrayData objectAtIndex:indexPath.row] objectForKey:@"ProductName"];
    }
    
    else if (a == 1){
        cell.titleLabel.text = [[zArrayImage objectAtIndex:indexPath.row] objectForKey:@"CategoryName"];
    }
    
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (a == 0) {
        DataTuiDetailsViewController *tuiVC = [[DataTuiDetailsViewController alloc] init];
        // self.hidesBottomBarWhenPushed = YES;
        tuiVC.ProductIDStr = [[zArrayData objectAtIndex:indexPath.row] objectForKey:@"ProductID"];
        tuiVC.ProductName =[[zArrayData objectAtIndex:indexPath.row] objectForKey:@"ProductName"];
        [self.navigationController pushViewController:tuiVC animated:YES];
    }else if (a == 1){
        DataImageViewController *imgVC = [[DataImageViewController alloc] init];
        imgVC.zName = [[zArrayImage objectAtIndex:indexPath.row] objectForKey:@"CategoryName"];
        imgVC.zCID = [[zArrayImage objectAtIndex:indexPath.row] objectForKey:@"CategoryID"];
        [self.navigationController pushViewController:imgVC animated:YES];
        
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

-(void)selectedSegment:(DZNSegmentedControl *)col{
    
    if (col.selectedSegmentIndex == 0) {
        
        
    }else{
        //        AccountHandleData *data = [[AccountHandleData alloc] init];
        //        [data downloadDataImag:self.view];
        //        data.delegate = self;
    }
    
    
    [self.tableView reloadData];
    
    
}

#pragma mark - segmentDelegate
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)view
{
    return UIBarPositionBottom;
}

-(void)dealloc{
    self.control = nil;
    self.tableView = nil;
    self.arrays = nil;
    self.imageArray = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
